找传奇、传世资源到传世资源站!

c# mvc 微信发送消息 示例代码下载

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

//******************************************************************************//文 件 名: WXAccessToken//版权所有: 蓝思创工作室//创 建 人: 蓝思创//创建日期: 2016-05-08//网 址:https://shop112893715.taobao.com///功能描述:获取AccessToken//--------------------------------------------------------------------------------//修改人://修改原因://修改日期:// 更多源码下载,请访问 https://shop112893715.taobao.com///******************************************************************************using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Configuration;using System.Net;using System.IO;using System.Text;public partial class WXAccessToken : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { } protected void BtnGet_Click(object sender, EventArgs e) { string AccessTokenJson = string.Empty; TextBoxAccessToken.Text = GetAccessToken(out AccessTokenJson); TextBoxJson.Text = AccessTokenJson; } #region 获取AccessToken /// <summary> /// 获取AccessToken /// </summary> /// <param name="corpid">企业Id</param> /// <param name="corpsecret">管理组的凭证密钥</param> /// <returns></returns> private static string GetAccessToken(out string AccessTokenJson) { //过期时间为7200秒 120分钟 string token = string.Empty; //session保存 if (System.Web.HttpContext.Current.Session["AccessToken"] == null) { string corpId = ConfigurationManager.AppSettings["CorpId"];//从配置文件获取corpId string corpsecret = ConfigurationManager.AppSettings["Secret"];//从配置文件获取corpId string Gurl = string.Format("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={0}&corpsecret={1}", corpId, corpsecret); AccessTokenJson = HttpGet(Gurl, ""); string regex = "\"access_token\":\"(?<token>.*?)\""; token = CRegex.GetText(AccessTokenJson, regex, "token"); System.Web.HttpContext.Current.Session["AccessTokenJson"] = AccessTokenJson; System.Web.HttpContext.Current.Session["AccessToken"] = token; System.Web.HttpContext.Current.Session.Timeout = 7200; } else { AccessTokenJson = System.Web.HttpContext.Current.Session["AccessTokenJson"].ToString(); token = System.Web.HttpContext.Current.Session["AccessToken"].ToString(); } return token; } #endregion #region GET请求与获取结果 /// <summary> /// GET请求与获取结果 /// </summary> private static string HttpGet(string Url, string postDataStr) { HttpWebRequest request; if (string.IsNullOrEmpty(postDataStr)) { request = (HttpWebRequest)WebRequest.Create(Url); } else { request = (HttpWebRequest)WebRequest.Create(Url (postDataStr == "" ? "" : "?") postDataStr); } request.Method = "GET"; request.ContentType = "text/html;charset=UTF-8"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8); string retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); myResponseStream.Close(); return retString; } #endregion}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复